package com.wl.util;

import com.wl.constant.Constant;
import com.wl.mapper.ArticlesMapper;
import com.wl.pojo.Articles;
import com.wl.service.impl.ArticlesServiceImpl;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: treeHole
 * @author: 吴磊
 * @create: 2021-07-24 18:30
 */
public class JsoupUtil {
    private ArticlesMapper articlesMapper = MybatisUtils.getMapper(ArticlesMapper.class);
    private ArticlesServiceImpl articlesService = new ArticlesServiceImpl();

    //测试数据
    @SneakyThrows
    @Test
    public void test1() {
        //获取请求
        String url = "http://www.6our.com/best?&p=2";
        // 解析网页 （Jsou返回的Document就是浏览器的Docuement对象）
        Document document = Jsoup.parse(new URL(url), 3000);
        Elements elementsByClass = document.getElementsByClass("col1");
        for (Element e : elementsByClass) {
            Elements elements = e.getElementsByClass("untagged");
            for (Element e1 : elements) {
                String content = e1.getElementsByClass("content").eq(0).html();
                String tags = e1.getElementsByClass("tags").eq(0).text();
                Elements a = e1.select("a[href]");
                System.out.println(a.toString());
//                System.out.println(content);
//                System.out.println(tags);
            }
        }
    }

    @SneakyThrows
    public static List<Map<String, Object>> getArticleByURL(String url) {
        List<Map<String, Object>> list = new ArrayList<>();
        Document document = Jsoup.connect(url).get();
        Elements elements = document.getElementsByClass("untagged");
        for (Element e : elements) {
            HashMap<String, Object> map = new HashMap<>();
            Elements select = e.getElementsByClass("content").eq(0);
//            String html = select.html();
            String html = select.text();
//            Elements as = select.get(0).select("a[href]");
//            for (Element e1 : as) {
//                String s = e1.toString();//完整的a标签
//                String text = e1.text();//a标签里面的字
//                html = html.replace(s, text);
////                System.out.println(s);
//            }
////            html = html.replaceAll("\\n", "");
//            html = html.replaceAll("<p><br></p>", "");
//            html = html.replaceAll("<p></p>", "");
//            String s1 = html.replaceAll("<p>\n</p>", "");
            map.put("html", html);
            String tags = e.getElementsByClass("tags").text();
            tags = tags.replaceAll("\\u00A0", " ");
            tags = tags.trim();
            if (tags.length() > 0) {
                ArrayList<String> tagList = new ArrayList<>();
                String[] strs = tags.split(" ");
                String tag = "";
                for (String s : strs) {
                    String trim = s.trim();
                    if (trim.length() > 0) {
                        tagList.add(trim);
                        tag += (trim + " ");
                    }
                }
                tag = tag.trim();
                map.put("tags", tagList);
                map.put("strTag", tag);
            }
            list.add(map);
        }
        return list;
    }


    @SneakyThrows
    public int getArticleByURLAuto(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String url = request.getParameter("url");
        int end = Integer.parseInt(request.getParameter("end"));
        Integer start = Integer.valueOf(request.getParameter("start"));
        Jedis jedis = RedisUtil.getJedis();
        int count = 0;
        for (int i = start; i <= end; i++) {
            try {
                String tempUrl = url + i;
                jedis.sadd(Constant.JSOUP_URL, tempUrl);
                Document document = Jsoup.connect(tempUrl).get();
                Elements elements = document.getElementsByClass("untagged");
                for (Element e : elements) {
                    Elements select = e.getElementsByClass("content").eq(0);
                    String articleContent = select.text();//文章内容
                    Articles articles = new Articles();
                    String articleId = MyUtil.getUUID();
                    articles.setArticleId(articleId);
                    articles.setArticleStatus("1");
                    articles.setArticleDate(MyUtil.getRandomStringTime());
                    articles.setUserId(userId);
                    articles.setArticleContent(articleContent);

                    String tags = e.getElementsByClass("tags").text();
                    tags = tags.replaceAll("\\u00A0", " ");
                    tags = tags.trim();
                    ArrayList<String> tagList = new ArrayList<>(); //List的标签，用来插入Redis
                    String tag = "";
                    if (tags.length() > 0) {
                        String[] strs = tags.split(" ");
                        for (String s : strs) {
                            String trim = s.trim();
                            if (trim.length() > 0) {
                                tagList.add(trim);
                                tag += (trim + " ");
                            }
                        }
                        articles.setArticleLabel(tag.trim());
                    } else {
                        articles.setArticleLabel("");
                    }
                    Integer integer = articlesMapper.insertByJsoup(articles);
                    if (integer > 0) {
                        //说明文章插入成功
                        count++;
                        jedis.zadd(Constant.INTEGRAL, 0, articleId);
                        if (!tagList.isEmpty()) {
                            tagList.forEach(label -> {
                                jedis.sadd(label, articleId);
                                jedis.sadd(Constant.ALL_LABELS, label);
                            });
                        }
                    }
                    articlesService.createIndex(articles);
                }
            } catch (Exception e) {
                continue;
            }
        }
        jedis.close();
        return count;
    }

}
